package com.example.passwordmanager;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.passwordmanager.Util.FingerprintUtil;
import com.example.passwordmanager.Util.Sys;
import com.example.passwordmanager.databinding.FragmentLockBinding;
import com.example.passwordmanager.dialog.FingerprintDialogFragment;
import com.example.util.base_util.SPUtil;


public class LockFragment extends Fragment {

    private View root;
    private FragmentLockBinding fragmentLockBinding;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_lock, container, false);
        fragmentLockBinding = FragmentLockBinding.bind(root);
        initView();
        return root;
    }
    private FingerprintDialogFragment fingerprintDialogFragment;
    private void initView() {
        fragmentLockBinding.imageClose.setOnClickListener(v -> {
            getActivity().finish();
        });
        fragmentLockBinding.imageNext.setOnClickListener(v -> {
            fragmentLockBinding.input.setErrorEnabled(false);
            String psd = fragmentLockBinding.etPassword.getText().toString();
            if (TextUtils.isEmpty(psd)) {
                fragmentLockBinding.input.setError("密码不能为空");
                return;
            }
            String s = (String) SPUtil.get(getContext(), "password", "");
            if (!TextUtils.equals(psd, s)) {
                fragmentLockBinding.input.setError("密码错误");
                return;
            }
            Toast.makeText(getContext(), "已解除锁定", Toast.LENGTH_LONG).show();
            SPUtil.put(getContext(), "isLock", false);
            getActivity().finish();

        });
        fragmentLockBinding.imageIcon.setOnClickListener(v->{
            if (Sys.fingerprintType==0&&FingerprintUtil.supportFingerprint(getContext())){
                if (!fingerprintDialogFragment.isAdded()){
                    fingerprintDialogFragment.show(getChildFragmentManager());
                }
            }
        });
        fingerprintDialogFragment=new FingerprintDialogFragment(new FingerprintDialogFragment.CallBack() {
            @Override
            public void onSucceed() {
                Toast.makeText(getContext(), "已解除锁定", Toast.LENGTH_LONG).show();
                SPUtil.put(getContext(), "isLock", false);
                getActivity().finish();
            }
        });



        long time= (long) SPUtil.get(getContext(),"time",0L);
        long t=System.currentTimeMillis();
        if (time+3600*1000*24>t){
            long l=(time+3600*1000*24-t)/1000;
            String s=(l/3600)+"小时"+(l%3600)/60+"分钟后可重置密码";
            fragmentLockBinding.tvLockTime.setText(s);

        }else {
            getActivity().finish();
            startActivity(new Intent(getContext(),MainActivity.class));
        }

    }
}